﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class active_pro_changepic : System.Web.UI.Page
{
    public string imageUrl = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private bool SaveImgae()
    {
        #region  保存图片
        HttpPostedFile file = fileRoom.PostedFile;
        string imgname = Path.GetFileName(file.FileName).ToLower();
        if (imgname == null || imgname.Length == 0)
        {
            Response.Write("<script>alert(\"请上传宣传2M以内的图片\");history.go(-1);</script>");
            return false;
        }
        if (file.ContentLength > 2 * 1024 * 1024)
        {
            Response.Write("<script>alert(\"请上传2M内的图片\");history.go(-1);</script>");
            return false;
        }
        imgname = imgname.Substring(0, imgname.Length - 4);
        string PicType = Path.GetExtension(file.FileName).ToLower();	//获得图片的扩展名
        if (PicType != ".jpg" && PicType != ".jpeg" && PicType != ".png")
        {
            Response.Write("<script>alert(\"只能上传格式为（'.jpg','.jpeg','.png'）\");history.go(-1);</script>");
            return false;
        }

        string PathUrl = "ActiveApply/images/";
        int SaveSizeW = 525;
        int SaveSizeH = 195;
        //客户端
        string phsavename = Request.Form["activename"].Trim()  + "" + DateTime.Now.ToString("yyyyMMdd") + PicType;
        string phserver = PathUrl + phsavename;  //Server.MapPath(PathUrl);
        string saveserver = "http://logpic.9158.com/" + PathUrl;
        //if (Directory.Exists(phserver) == false)  //如果不存在就创建file文件夹
        //{
        //    Directory.CreateDirectory(phserver);
        //}
        try
        {
            using (System.Drawing.Image originalImage = System.Drawing.Image.FromStream(file.InputStream))
            {
                System.Drawing.Bitmap bitmap2 = new System.Drawing.Bitmap(originalImage, SaveSizeW, SaveSizeH);
                bitmap2.Save(@"\\10.191.231.224\dynamic2\roomlogpic\ActiveApply\images\" + phsavename);
            }
        }
        catch (System.NullReferenceException)
        {
            Response.Write("<script>alert(\"图片上传失败\");history.go(-1);</script>");//上传失败
            return false;
        }
        imageUrl = saveserver + phsavename;
        return true;
        #endregion
    }

    protected void Button1_ServerClick(object sender, EventArgs e)
    {
        SaveImgae();
    }
}
